package com.sinodata.bsm.cicp.collector.common;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * <p>
 * Description: 
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-11-12 15:36:25      liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class SocketCollection extends Collector {

    private static final Logger logger = Logger.getLogger(Collector.class);

    /**
     *
     * @param resAtts ResAttributeValValue[]
     * @param ip String
     * @return List
     */
    @Override
    public List<ResPropertyValue> collect(String ip) {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        String ipString = this.getParamMap().get("IP");
        propertyValueX.setValues(propValues);
        values.add(propertyValueX);
        try {
            InetAddress ipAddress = InetAddress.getByName(ipString);
            int port = Integer.parseInt(this.getParamMap().get("PORT"));
            String state = getSocketConnection(ipAddress, port);
            propValues[0] = ResPropertyValue.createPropValue(state, propMetas[0]);
        } catch (Exception e) {
            propValues[0] = ResPropertyValue.createPropValue("False", propMetas[0]);
        }
        propertyValueX.setValues(propValues);
        values.add(propertyValueX);
        return values;
    }

    /**
     *
     * @param ip InetAddress
     * @param port int
     * @return List
     * @throws IOException 
     */
    public String getSocketConnection(InetAddress ip, int port) throws IOException {
        if (ip == null || port <= 0) {
            logger.error("Test Connect Web Server Error, URL is NULL.");
            return null;
        }
        Socket socket = null;
        String res = "False";
        for (int i = 0; i < 2; i++) {//如果不通，尝试2次
            try {
                socket = new Socket();
                SocketAddress addr = new InetSocketAddress(ip, port);
                socket.connect(addr, 10000);
                if (socket != null) {
                    res = "True";
                    break;
                }
            } catch (IOException e) {
                continue;
            }
        }
        try {
            socket.close();
        } catch (IOException e) {
            logger.error("failed to close socket", e);
        } finally {
            socket.close();
        }
        return res;
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "IP", "PORT" };
    }

    @Override
    public String[] getPropMeta() {
        //        return new String[] { "端口状态" };
        return new String[] { "Port_Status" };
    }

}
